<?php

namespace Plu\Library\Models\Platform\Admire;

use Phalcon\Mvc\Model,
    Phalcon\Db\Column;

class Admire extends Model{

    public $Id;

    public $Type;

    public $Guid;

    public $Counts;

    public $UpdateTime;

    /**
     * 点赞对应的常量类型
     */
    const AdmireType = array(
        'video'     => 0,
        'picture'   => 1,
        'article'   => 2,
        'news'      => 3
    );

    public function initialize() {
        $this->setReadConnectionService('db.platform.slave');
        $this->setWriteConnectionService('db.platform');
    }

    public function getSource() {
        return "Admire";
    }

    /**
     * @param $guid
     * @return array
     * 通过guid获取对应的点赞数
     */
    public function getItem($type, $guid) {
        return self::findFirst(array(
            "Guid = :guid: AND Type = :type:",
            "bind" => array(
                "guid" => $guid,
                "type" => $type
            ),
            'bindTypes' => array(
                'guid'	=> Column::BIND_PARAM_INT,
                'type'	=> Column::BIND_PARAM_INT,
            )
        ));
    }

    /**
     * @param $admire
     * 实时更新点赞数
     */
    public function updateItemCount($type, $guid, $counts = 0){
        $item = $this->getItem($type, $guid);
        $now = date("y-m-d h:i:s", time());
        if ($item === false) {
            $admire = new self;
            $admire->Type   = $type;
            $admire->Guid   = $guid;
            $admire->Counts = $counts;
            $admire->UpdateTime = $now;

            $admire->create();
        }else{
            $item->Counts     = $item->Counts + $counts;//累加数值
            $item->UpdateTime = date("y-m-d h:i:s");
            $item->update();
        };
    }
}

?>
